﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web;

namespace MyVoteMVC.Models
{
    public class User
    {
        public User()
        {
            Tickets = new List<Ticket>();
        }
        /// <summary>
        /// PK Guid
        /// </summary>
        public string User_ID { get; set; }

        /// <summary>
        /// 用户名(唯一)
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 用户名(不唯一)
        /// </summary>
        public string RealName { get; set; }

        /// <summary>
        /// 用户邮箱地址(唯一)
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// 用户密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 是否管理员
        /// </summary>
        public int? IfAdminValue { get; set; }
        public UserType IfAdmin
        {
            get
            {
                return (UserType)IfAdminValue;
            }
            set
            {
                IfAdminValue = (int)value;
            }
        }
        /// <summary>
        /// 是否领导
        /// </summary>
        public int? IfLeaderValue { get; set; }
        public UserRole IfLeader
        {
            get
            {
                return (UserRole)IfLeaderValue;
            }
            set
            {
                IfLeaderValue = (int)value;
            }
        }

        /// <summary>
        /// 创建人
        /// </summary>
        public string Created_By { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? Created_Date { get; set; }

        /// <summary>
        /// 创建方法
        /// </summary>
        public string Created_Action { get; set; }

        /// <summary>
        /// 更新人
        /// </summary>
        public string Last_Update_By { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? Last_Update_Date { get; set; }

        /// <summary>
        /// 更新方法
        /// </summary>
        public string Last_Update_Action { get; set; }

        //LazyLoad
        /// <summary>
        /// 用户相关的票据
        /// </summary>
        public virtual ICollection<Ticket> Tickets { get; set; }

    }

    /// <summary>
    /// 用户类型
    /// </summary>
    public enum UserType
    {
        /// <summary>
        /// 普通用户
        /// </summary>
        User = 0,
        /// <summary>
        /// 管理员用户
        /// </summary>
        Admin = 1
    }

    /// <summary>
    /// 用户角色
    /// </summary>
    public enum UserRole
    {
        /// <summary>
        /// 经办人
        /// </summary>
        Transactor = 0,
        /// <summary>
        /// 领导
        /// </summary>
        Leader = 1
    }

    #region Services

    public interface IFormsAuthenticationService
    {
        void SignIn(string UserName, string UserInfo);
        void SignOut();
    }

    public class FormsAuthenticationService : IFormsAuthenticationService
    {
        public void SignIn(string UserName, string UserInfo)
        {
            if (String.IsNullOrEmpty(UserName)) throw new ArgumentException("Value cannot be null or empty.", "UserName");

            //FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddDays(365), true, UserInfo);
            string enTicket = FormsAuthentication.Encrypt(ticket);
            HttpContext.Current.Response.Cookies.Add(new HttpCookie(".ASPXAUTH", enTicket));
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }
    #endregion   
}
